package com.hzya.frame.system.module.service.impl;

import com.hzya.frame.system.module.dao.IModuleDao;
import com.hzya.frame.system.module.entity.ModuleEntity;
import com.hzya.frame.system.module.service.IModuleService;
import com.hzya.frame.utils.common.Constants;
import com.hzya.frame.utils.core.action.SessionContext;
import com.hzya.frame.utils.core.service.BaseService;
import com.hzya.frame.utils.core.tree.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @description: 模块  service
 * @tableName:   system_module 
 * @entityName:  ModuleEntity
 * @author: 	 gjh
 * @history:	 1.0
 */
@Service(value="system_moduleService")
public class  ModuleServiceImpl extends BaseService< ModuleEntity,Long> implements IModuleService{
	
	protected IModuleDao  moduleDao;
	
	@Autowired
	public void setModuleDao(IModuleDao dao) {

		this.moduleDao = dao;
		this.dao=dao;
	}

	@Override
	public List<TreeNode> createModuleTree(ModuleEntity entity) throws Exception {
		entity.setLvl("1");
		entity.setSort("order by ");
		entity.setSort("order_num asc");
		List<ModuleEntity> tempList = moduleDao.query(entity);
		List<TreeNode> rootNode = null;
		if (tempList != null) {
			rootNode = new ArrayList<TreeNode>();
			for (ModuleEntity tempDomain : tempList) {
				/** 查询子节点 */
				TreeNode children = getChildrens(tempDomain);
				children.setId(tempDomain.getId());
				children.setText(tempDomain.getName());
				children.setAttributes(tempDomain);
				children.setState("closed");
				children.setParentId(tempDomain.getUp_id());
				rootNode.add(children);
			}
		}
		return rootNode;

	}
	

	/**
	 * 根据当前这个节点查询子节点。。
	 * 
	 * @param modul
	 * @return
	 */
	private TreeNode getChildrens(ModuleEntity modul) {
		/** new一个返回对象， */
		TreeNode node = new TreeNode();
		/** 查询当前这个节点下面有没有子节点 */
		ModuleEntity childernNode = new ModuleEntity();
		childernNode.setUp_id(modul.getId());
		childernNode.setSort("order by ");
		childernNode.setSort("order_num asc");
		List<ModuleEntity> tempList = moduleDao.query(childernNode);
		/** 如果没有子节点，把当前这个节点返回 */
		if (tempList == null || tempList.size() == 0) {
			node.setId(modul.getId());
			node.setText(modul.getName());
			node.setParentId(modul.getUp_id());
			node.setAttributes(modul);
		} else {
			/** 此处表明是有节点的 */
			for (ModuleEntity mo : tempList) {
				TreeNode grandsonNode = getChildrens(mo);
				grandsonNode.setId(mo.getId());
				grandsonNode.setText(mo.getName());
				grandsonNode.setAttributes(mo);
				grandsonNode.setState("closed");
				grandsonNode.setParentId(mo.getUp_id());
				node.getChildren().add(grandsonNode);
			}
		}
		return node;
	}
	
	/**
	 * 判断当前节点有没有子节点，如果有，给父节点负值
	 * 
	 * @param entity
	 * @return
	 */
	private List<TreeNode> getChildren(ModuleEntity entity) {
		entity.setUp_id(entity.getId());
		entity.setLvl("1");
		entity.setSort("order by ");
		entity.setSort("order_num asc");
		List<ModuleEntity> tempList = moduleDao.query(entity);
		List<TreeNode> childrens = null;
		if (tempList != null && tempList.size() > 0) {
			childrens = new ArrayList<TreeNode>();
			for (ModuleEntity mo : tempList) {
				TreeNode node = new TreeNode();
				node.setId(mo.getId());
				node.setText(mo.getName());
				node.setAttributes(mo);
				childrens.add(node);
			}
		}
		return childrens;
	}

	@Override
	public List<TreeNode> createSubModuleTree(ModuleEntity entity) {
		entity.setSort("order by ");
		entity.setSort(" order_num asc");
		List<ModuleEntity> tempList = moduleDao.query(entity);
		List<TreeNode> childNode = new ArrayList<TreeNode>();
		for (ModuleEntity tempDomain : tempList) {
			TreeNode tempNode = new TreeNode();
			tempNode.setId(tempDomain.getId());
			tempNode.setText(tempDomain.getName());
			tempNode.setAttributes(tempDomain);
			tempNode.setParentId(tempDomain.getUp_id());
			tempNode.setState("open");
			childNode.add(tempNode);
		}
		return childNode;
	}

	/***
	 * 根据模块id创建缺省按钮 （新增，编辑，删除，查询） 权限按钮会使用
	 * @param entity
	 */
	@Override
	public void createDefaultButton(ModuleEntity entity) {
		/** 先删除原有的按钮，在新增*/
		ModuleEntity loginEntity = new ModuleEntity();
		loginEntity.setType("button");
		loginEntity.setUp_id(entity.getId());
		moduleDao.logicRemoveMultiCondition(loginEntity);
		Map<String, String> buttonsHtml= Constants.DEFAULT_BUTTON_HTML;
		AtomicReference<Integer> i= new AtomicReference<>(2);

		buttonsHtml.forEach((key,value)->{

			ModuleEntity moduleEntity = new ModuleEntity();
				moduleEntity.setUp_id(entity.getId());
				moduleEntity.setUp_name(entity.getName());
				moduleEntity.setHtm_content(value);
				moduleEntity.setName(key);
				moduleEntity.setType("button");
				moduleEntity.setLevel("admin");
				moduleEntity.setUrl("空");
				moduleEntity.setControlle_address("空");
				moduleEntity.setLvl("4");
				moduleEntity.setOrder_num(i.get().longValue());
				moduleEntity.setUrl("空");
				moduleEntity.setCreate_user_id(SessionContext.getCurrentUser().getId());
				moduleDao.save(moduleEntity);
				i.getAndSet(i.get() + 1);
		});
	}
}
